<style scoped>
.line {
  text-align: center;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.tab-container {
  text-align: center;
  margin-bottom: 20px;
}

.app-container {
  font-size: 14px;
}

.link-type {
  color: #337ab7;
  cursor: pointer;
  text-decoration: underline;
}

.job-title {
  display: inline-block;
  height: 38px;
  line-height: 38px;
}

.job-title * {
  margin-right: 6px;
}

.job-control {
  display: inline-block;
  float: right;
  padding: 3px 0;
}

.el-card__header {
  padding: 10px 20px;
}

ul li {
  margin-bottom: 10px;
}

.job-form {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-between;
}

.job-form .left-option{
  display: block;
}

.job-paragraph {
  margin-bottom: 20px;
  background-color: #f8f8f8;
  padding: 10px;
  border-radius: 10px;
  border: 1px dashed #ccc;
}

.job-paragraph .header {
  margin-bottom: 8px;
}

.paragraph-header {
  float: right;
}
</style>

<template>
  <div class="app-container">
    <div class="tab-container">
      <el-radio-group v-model="job.type" size="small">
        <el-radio-button label="SPIDER" disabled>站外招聘</el-radio-button>
        <el-radio-button label="ORIGINAL">原创招聘</el-radio-button>
        <el-radio-button label="GOVERNMENT">公职招聘</el-radio-button>
      </el-radio-group>
    </div>
    <el-card class="box-card">
      <div class="job-form">
        <div class="left-option">
          <el-input
            size="small"
            v-model="job.title"
            placeholder="招聘标题"
            style="width: 280px;"
          ></el-input>
          <el-input
            size="small"
            v-model="job.author"
            v-if="job.type == 'ORIGINAL'"
            placeholder="企业名称"
            style="width:150px;"
          ></el-input>
        </div>
        <div class="right-option">
          <el-date-picker
            :disabled="job.type != 'GOVERNMENT'"
            v-model="job.createTime"
            placeholder="招聘创建时间"
            style="width: 140px;"
            size="small"
            type="date"
            title="招聘创建时间"
          ></el-date-picker>
          <el-date-picker
            v-model="job.cutoffTime"
            placeholder="招聘截止时间"
            style="width: 140px;"
            size="small"
            type="date"
            title="招聘截止时间"
          ></el-date-picker>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-success"
            :loading="createLoading"
            @click="createJob"
            >确认提交</el-button
          >
        </div>
      </div>
      <el-row :gutter="20">
        <el-col :span="14">
          <div
            class="job-paragraph"
            v-for="(paragraph, index) in job.paragraphList"
            :key="index"
          >
            <div class="header"  v-if="job.type === 'GOVERNMENT'">
              <el-input
                size="small"
                v-model="paragraph.title"
                style="width: 250px;"
              ></el-input>
              <div class="paragraph-header">
                <el-button
                  icon="el-icon-circle-plus-outline"
                  type="text"
                  size="small"
                  title="添加段落"
                  @click="addParagraph(index)"
                ></el-button>
                <el-button
                  icon="el-icon-delete"
                  type="text"
                  size="small"
                  title="删除段落"
                  @click="removeParagraph(index)"
                ></el-button>
              </div>
            </div>
            <div class="context">
              <tinymce-editor
                :initData="init"
                ref="editor"
                v-model="paragraph.context"
              ></tinymce-editor>
            </div>
          </div>
        </el-col>
        <el-col :span="10">
          <div class="preview">
            <PreviewGoverment v-if="job.type === 'GOVERNMENT'" :job="job"></PreviewGoverment>
            <PreviewOriginal v-if="job.type === 'ORIGINAL'" :job="job"></PreviewOriginal>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import TinymceEditor from "../../components/tinymce-editor";
import PreviewGoverment from "./previewGoverment";
import PreviewOriginal from "./previewOriginal";
import axios from "axios";

const axiosForm = axios.create({
  timeout: 1000 * 60 * 8, // request timeout
  headers: {
    "Content-Type": "application/json"
  }
});

export default {
  components: {
    TinymceEditor,
    PreviewGoverment,
    PreviewOriginal
  },
  data() {
    return {
      STATUS_MAP: {
        INIT: {
          text: "未审核",
          color: "#3A71A8"
        },
        IGNORE: {
          text: "忽略",
          color: "#909399"
        },
        CONFIRM: {
          text: "审核通过",
          color: "#4AB7BD"
        }
      },
      job: {
        oid: null,
        title: "招聘标题",
        author: "岑人启事招聘小助手",
        comment_list: [],
        createTime: new Date(),
        cutoffTime: null,
        detail_dom: "",
        link: null,
        status: "CONFIRM",
        text: null,
        paragraphList: [
          {
            title: "段落标题",
            context: "段落内容"
          }
        ],
        type: "ORIGINAL"
      },
      init: {
        language_url: "/tinymce/langs/zh_CN.js", // 语言包的路径
        language: "zh_CN", //语言
        skin_url: "/tinymce/skins/ui/oxide", // skin路径
        height: 150, //编辑器高度
        branding: false, //是否禁用“Powered by TinyMCE”
        menubar: false //顶部菜单栏显示
      },
      createLoading: false
    };
  },
  methods: {
    addParagraph(i) {
      this.job.paragraphList.splice(i + 1, 0, {
        title: "新段落标题",
        context: "新段落内容"
      });
    },
    removeParagraph(i) {
      if (this.job.paragraphList.length <= 1) {
      }

      this.$confirm("确认删除段落？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.job.paragraphList.splice(i, 1);
      });
    },
    getJobDetail() {
      axiosForm
        .get("http://127.0.0.1:7001/showJobByOid", {
          params: {
            oid: this.oid
          }
        })
        .then(resp => {
          let respData = resp.data.data[0];
          this.job = respData;
          this.job.cutoffTime = new Date(respData.cutoffTime);
          this.comment_list = respData.comment_list.splice(0, 1);
        });
    },
    createJob() {
      this.$confirm("确定发布招聘信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // this.job.status = "CONFIRM";
        this.job.oid = "GOV_" + new Date().getTime();
        this.createLoading = true;
        axiosForm
          .post("http://127.0.0.1:7001/job/create", this.job)
          .then(() => {
            this.createLoading = false;
            this.$message({
              type: "success",
              message: "发布成功！"
            });
            // 跳转到招聘列表页
            this.$router.push('/dashboard');
          })
          .catch(err => {
            console.log(err);
            this.createLoading = false;
            this.$message({
              type: "error",
              message: "发布失败！"
            });
          });
      });
    }
  },
  created() {
    // this.oid = this.$route.params.oid;
    // this.getJobDetail();
  },
  mounted() {
    // tinymce.init(this.init);
  }
};
</script>
